package cn.qiyu5522.tk_ems.controller.admin;


import cn.qiyu5522.tk_ems.entity.JobCategory;
import cn.qiyu5522.tk_ems.entity.dto.JobCategoryDTO;
import cn.qiyu5522.tk_ems.entity.dto.QueryParamDTO;
import cn.qiyu5522.tk_ems.myanotation.WebLog;
import cn.qiyu5522.tk_ems.result.R;
import cn.qiyu5522.tk_ems.service.JobCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Tonkey
 * @since 2023-02-22
 */
@Slf4j
@Api(tags = "工作类别管理")
@RestController
@RequestMapping("admin/jobCategory")
public class JobCategoryController {

    @Resource
    private JobCategoryService jobCategoryService;

    @WebLog(description = "分页条件查询工作类别列表")
    @ApiOperation("工作类别列表查询")
    @ApiImplicitParam(name = "queryParamDTO", value = "条件查询对象", required = true, dataType = "QueryParamDTO", dataTypeClass = QueryParamDTO.class)
    @PostMapping("/query")
    public R query(@Validated(QueryParamDTO.Query.class) @RequestBody QueryParamDTO<JobCategoryDTO> queryParamDTO) {
        return jobCategoryService.queryJobCategoryByPageCondition(queryParamDTO);
    }
    @WebLog(description = "工作类别新增--批量")
    @ApiOperation("工作类别新增")
    @ApiImplicitParam(name = "JobCategoryDTOs", value = "工作类别新增", required = true,dataType = "List",dataTypeClass = List.class)
    @PostMapping("/add")
    public R add(@Validated(JobCategory.Insert.class) @RequestBody List<JobCategoryDTO> JobCategoryDTOs) {
        return jobCategoryService.saveMore(JobCategoryDTOs);
    }
    @WebLog(description = "工作类别编辑")
    @ApiOperation("工作类别编辑")
    @ApiImplicitParam(name = "jobCategoryDTO", value = "工作类别实体类对象", required = true, defaultValue = "{}", dataTypeClass = JobCategoryDTO.class)
    @PutMapping("/edit")
    public R edit(@Validated(JobCategory.Update.class) @RequestBody JobCategoryDTO jobCategoryDTO) {
        JobCategory jobCategory = new JobCategory();
        BeanUtils.copyProperties(jobCategoryDTO,jobCategory);
        boolean b = jobCategoryService.updateById(jobCategory);
        return b ? R.ok().message("编辑成功") : R.error().message("编辑失败");
    }
    @WebLog(description = "工作类别删除")
    @ApiOperation("工作类别删除")
    @ApiImplicitParam(name = "ids", value = "工作类别编号集合", required = true, defaultValue = "[]", dataTypeClass = List.class)
    @PostMapping("/remove")
    public R remove(@RequestBody List<Long> ids) {
        boolean b = jobCategoryService.removeByIds(ids);
        return b ? R.ok().message("删除成功!"):R.error().message("删除失败");
    }

    @WebLog(description = "获取工作大类别")
    @ApiOperation("获取工作大类别")
    @GetMapping("/queryForCategoryList")
    public R queryForCategoryList() {
        return jobCategoryService.queryForCategoryList();
    }

    @ApiOperation("检测班级名称是否重复")
    @ApiImplicitParam(name="name",value = "职位名称名称", required = true, dataType = "String")
    @GetMapping("/queryForCheckRepeatName/{name}")
    public R queryForCheckRepeatName(@NotBlank(message = "查重时，职位名称不能为空") @PathVariable String name){
        return jobCategoryService.queryByName(name);
    }

    @ApiOperation("根据pid获取职位列表")
    @ApiImplicitParam(name="pid",value = "父级id-分类", required = true, dataType = "Long")
    @GetMapping("/queryJobNameListByPid/{pid}")
    public R queryJobNameListByPid(@NotBlank(message = "请先选择分类") @PathVariable Long pid){
        return jobCategoryService.queryJobNameAndIdListByPid(pid);
    }


}
